package me.magicall.test.net.download._24meinv;

import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import me.magicall.net.download.Download;
import me.magicall.net.http.UrlConnectorConfig;
import me.magicall.util.kit.Kits;
import me.magicall.util.time.TimeFormatter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Fetcher {

	File saveDir;

	public void fetch() {
		final List<String> urls = Arrays.asList(Const.URLS);
		for (String url : urls) {//摄影机构 "http://www.24meinv.com/gaoqing/rq-star_1.html"
			System.out.println("@@@@@@" + url);
			final int allPages = renwu1(url);
			for (int i = 2; i <= allPages; i++) {//机构的作品集s分页
				final String renwu = Kits.STR.subStringAfterLastSeq(url, "/");
				final String pageStr = Kits.STR.middle(renwu, "_", true, ".html", true);
				url = url.replace(pageStr, "_" + i + ".html");
				renwu(url);
			}
		}
	}

	public Document renwu(final String url) {// "http://www.24meinv.com/gaoqing/rq-star_1.html"
		final Document doc = doc(url);
		if (doc == null) {
			return null;
		}
		final Element tpsDiv = doc.select("div.tps").first();
		if (tpsDiv == null) {
			System.out.println("@@@@@@div.tps null:" + url);
			return null;
		}
		final Elements lis = tpsDiv.select("ul > li");
		if (lis == null) {
			System.out.println("@@@@@@lis null:" + url);
			return null;
		}
		for (final Element li : lis) {//机构的作品集s
			final Element a = li.child(0);
			if (a == null) {
				System.out.println("@@@@@@a null:" + url + " li:" + li);
				continue;
			}
			final String href = a.attr("href");
			if (Kits.STR.isEmpty(href)) {
				System.out.println("@@@@@@href empty:" + url + " a:" + a);
				continue;
			}
			final int allPages = taotu1(href);
			for (int i = 1; i < allPages; i++) {//套图中的图片分页
				taotu(Kits.STR.subStringBefore(href, ".html", true) + "_" + i + ".html");
			}
		}
		return doc;
	}

	public int renwu1(final String url) {// "http://www.24meinv.com/gaoqing/rq-star_1.html"
		final Document doc = renwu(url);
		if (doc == null) {
			return 0;
		}
		//总页数
		final Element pageDiv = doc.select("div.page").first();
		if (pageDiv == null) {
			System.out.println("@@@@@@renwu page div null:" + url);
			return 0;
		}
		final String pageText = pageDiv.ownText();
		if (Kits.STR.isEmpty(pageText)) {
			System.out.println("@@@@@@renwu page text empty:" + url);
			return 0;
		}
		final String allPageStr = Kits.STR.middle(pageText, "页/共", false, "页", false);
		final Integer allPage = Kits.INT.fromString(allPageStr.trim());
		if (allPage == null) {
			System.out.println("@@@@@@all page count null:" + url);
			return 0;
		}
		return allPage;
	}

	public void taotu(final String url) {//http://www.24meinv.com/gaoqing01/meinv5934.html
		final Document doc = doc(url);
		if (doc == null) {
			return;
		}
		taotu0(doc);
	}

	private void taotu0(final Document doc) {
		final Elements picLis = doc.select("div.gtps > ul > li");
		if (picLis == null) {
			System.out.println("@@@@@@taotu picLis null:" + doc.baseUri());
		}
		for (final Element pic : picLis) {//套图中的每一张图片
			final Element picA = pic.child(0);
			final String picUrl = picA.attr("href");
			if (picUrl == null) {
				System.out.println("@@@@@@taotu picUrl null:" + doc.baseUri() + " a:" + picA);
			}
			pic(picUrl);
		}
	}

	public int taotu1(final String url) {//http://www.24meinv.com/gaoqing01/meinv5934.html
		final Document doc = doc(url);
		if (doc == null) {
			return 0;
		}
		//创建保存目录
		final String name;
		final Element navElement = doc.select("div.weizhi > span.fl").first();
		if (navElement != null) {
			final String navString = navElement.ownText();
			if (!Kits.STR.isEmpty(navString)) {
				name = navString.substring(navString.lastIndexOf("→") + "→".length()).trim();
			} else {
				name = TimeFormatter.Y4M2D2H2MIN2S2MS3.format(new Date());
			}
		} else {
			name = TimeFormatter.Y4M2D2H2MIN2S2MS3.format(new Date());
		}

		saveDir = new File(Const.SAVE_DIR + name);
		if (!saveDir.exists()) {
			saveDir.mkdirs();
			System.out.println(saveDir.getAbsolutePath());
		}

		taotu0(doc);

		final Elements pageAs = doc.select("div.page > a");
		if (pageAs == null) {
			System.out.println("@@@@@@page as null:" + url);
			return 0;
		}
		final int size = pageAs.size();//倒数第一个是"最后一页",倒数第二个是"下一页",倒数第三个是最后一页的页码
		if (size < 3) {
			System.out.println("@@@@@@page as size <3:" + url);
			return 0;
		}
		final Element lastPaegA = pageAs.get(size - 3);
		if (lastPaegA == null) {
			System.out.println("@@@@@@last page null:" + url);
			return 0;
		}
		final Integer allPage = Kits.INT.fromString(lastPaegA.ownText().trim());
		if (allPage == null) {
			System.out.println("@@@@@@all page count null:" + url);
			return 0;
		}
		return allPage;
	}

	public void pic(final String url) {//http://www.24meinv.com//2011/8-13/meinv5934_2.html
		final Document doc = doc(url);
		if (doc == null) {
			return;
		}
		final Element imgElement = doc.select("#imgshow img").first();
		if (imgElement == null) {
			System.out.println("@@@@@@pic null:" + url);
			return;
		}
		final String src = imgElement.attr("src");//http://img.24meinv.com/hdmvs/RQ-STAR/NO.00526/002.jpg
		if (Kits.STR.isEmpty(src)) {
			System.out.println("@@@@@@pic src empty:" + url + " img:" + imgElement);
			return;
		}

		final String fileName = Kits.STR.subStringBefore(Kits.STR.subStringAfterLastSeq(url, "/"), ".html", true);
		final File file = new File(saveDir, fileName + ".jpg");
		if (file.exists()) {//已经下过了
			return;
		}
		System.out.println(file.getName());

		final UrlConnectorConfig config = new UrlConnectorConfig();
		config.setUrl(src);
//		config.setMetaType(MetaType.JPG);
//		config.setConnectTimeout((int) TimeUtil.MINUTE);
//		config.setCookies(null);
//		config.setReadTimeout((int) TimeUtil.MINUTE);
		final Download download = new Download(config);
		download.setTarget(file);
		download.download();
	}

	private static Document doc(final String url) {
		try {
			return Jsoup.connect(url.startsWith(Const.WEB) ? url : Const.WEB + url).get();
		} catch (final Exception e) {
			e.printStackTrace();
			System.out.println("@@@@@@doc null:" + url);
			return null;
		}
	}

	public static void main(final String[] args) {
		new Fetcher().fetch();
	}
}
